

;(function($){
	$.extend($.fn,{
		shoping:function(options){
			var self=this,
				$shop=$('#J-shoping-title');// 购物车的位置
			var S={
				init:function(){
					$(self).data('click',true).on('click',this.addShoping);//绑定按钮click
				},
				//采购  
				
				addShoping:function(e){
					//判断
					var newAmount = 0;
					function checkInput(id,productCode,oldamount){
						  if(oldamount==0){
							  layer.msg('无货',1, {type: 8,shade: [0.2, '#ccc']});
							  return false;
						  }  
					      var amount = $("#amount_"+id).val();
					      if(amount==""){
					    	  layer.msg('请输入购买数量！',1, {type: 5,shade: [0.2, '#ccc']});
					    	  $("#amount_"+id).focus();
					          return false;
					      }
					      if(!(/^(\+|-)?\d+$/.test(amount)) || amount < 0){
					    	  layer.msg('数量必须是正整数！',1, {type: 5,shade: [0.2, '#ccc']});
					          $("#amount_"+id).val("");
					          $("#amount_"+id).focus();
					          return false;
					      }else{
					    	 newAmount = amount;
					         return true;
					           
					      }
					}
					e.stopPropagation();
					var $target=$(e.target),
						id=$target.attr('id'),
						dis=$target.data('click'),
					    x = $target.offset().left + 30,
						y = $target.offset().top + 10,
						X = $shop.offset().left+$shop.width()/2-$target.width()/2+10,
						Y = $shop.offset().top;
					
						
					if(dis){
						if(checkInput($(this).attr("indexId"), $(this).attr("productCode"), $(this).attr("amount"))){
							if ($('#floatOrder').length <= 0) {
								$('body').append('<div id="floatOrder"><img src="'+$(this).attr("img")+'" width="100" height="100" /></div');
							}
							var $obj=$('#floatOrder');
							var param = {"productCode":$(this).attr("productCode"),"amount":newAmount};
							if(!$obj.is(':animated')){
								$obj.css({'left': x,'top': y}).animate({'left': X-100,'top': Y+30},500,function() {
									$obj.stop(false, false).animate({'left': X-70,'opacity':0},500,function(){
										//ajax 加入数据库
										$.ajax({
								               type: "post",
								               url: contentPath+"/shoppingCart/ajaxAddShoppingCart.s",
								               data:param, 
								               dataType:"json",
								               beforeSend:function(){
								            		
								               },
								               success: function(data){
								            	    $obj.remove();	
								                    if(data.code=="100"){
								                       
								                       if(data.result=="请先登录采购商!"){
								                    	   layer.login();  
								                       }else{
								                    	   layer.alert(data.result, 8);  
								                       }
								                	 }else{
								                    	$target.data('click',false).addClass('dis-click');
								                    	num=Number($(".cart-num").text());
														$(".cart-num").text(num+1);
								                	   layer.msg(data.result,1, {type: 1,shade: [0.2, '#ccc']});
								                    }
								               }
								           });  
									});
								});	
							}
					      	 
						}
						
					
						
						
					}
				}
				
			};
			S.init(); 
		}
	});	
})(jQuery);

